<?php

namespace app\company\controller;

use app\admin\model\department\Lists;
use app\admin\model\money\Change;
use app\admin\model\month\Salary;
use app\admin\model\product\Category;
use app\admin\model\product\Edit;
use app\admin\model\product\Listo;
use app\admin\model\user\Sign;
use app\common\controller\CompanyApi;
use app\common\controller\RedisCluster;
use think\Db;
use think\Exception;

/**
 * 首页接口
 */
class Productedit extends CompanyApi
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $pageSize = $this->request->post('page_size', 15);
        $list = Edit::where('user_id', $this->auth->id)
//            ->field('id,realname,mobile,area,address,status')
            ->order('id desc')
            ->paginate($pageSize);
        if ($list) {
            foreach ($list as $val) {
                $val->reply_image = make_full_url($val->reply_image);
                $val->review_image = make_full_url($val->review_image);
            }
        }

        $this->success('盘点表为', $list);
    }

    /**
     * 物资入库
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function create()
    {
        $product_id = $this->request->post('product_id');
        $total = $this->request->post('total');
        $remark = $this->request->post('remark');
        $reply_image = $this->request->post('reply_image');
        if (!$product_id || !$total || !$remark || !$reply_image) {
            $this->error('物资名称，数量，入库说明，签名缺一不可');
        }

        $user_id = $this->auth->id;

        $row = \app\admin\model\product\Lists::where('id', $product_id)
            ->find();
        if (!$row) {
            $this->error('该物资不存在！');
        }

        $data = [
            'product_id' => $product_id,
            'before' => $row->stock,
            'after' => $total,
            'user_id' => $user_id,
            'reply_image' => $reply_image,
            'remark' => $remark,
            'status' => 1
        ];

        $ret = \app\admin\model\product\Edit::create($data);
        if (!$ret) {
            $this->error('登记失败');
        }

        $this->success('登记成功，待审批');
    }

    /**
     * 获取登记详情
     */
    public function read()
    {
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数缺失！');
        }
        $row = \app\admin\model\product\Edit::find($id);
        if (!$row) {
            $this->error('该物资盘点登记不存在！');
        }

        $row->reply_image = make_full_url($row->reply_image);

        $row->review_image = $row->review_image ? make_full_url($row->review_image) : '';

        $user = \app\admin\model\User::field('username,nickname')->find();
        $row->user = $user;

        $this->success('登记详情', $row);
    }

    /**
     * 入库审批
     */
    public function audit()
    {
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数缺失！');
        }
        $row = \app\admin\model\product\Edit::find($id);
        if (!$row) {
            $this->error('该物资盘点登记不存在！');
        }

        if ($row->status != 1) {
            $this->error('该物资盘点登记已审批！');
        }

        $userId = $this->auth->id;
        $category_id = \app\admin\model\product\Lists::where('id', $row->product_id)->value('category_id');
        $ret = $this->checkLeaderAuth($userId, $category_id);
        if ($ret['code'] == 0) {
            $this->error($ret['msg']);
        }

        $review_image = $this->request->post('review_image');
        $review_remark = $this->request->post('review_remark');
        $status = $this->request->post('status');
        if (!$review_image) {
            $this->error('审核人员必须签名！');
        }

        if (!in_array($status, [5, 10])) {
            $this->error('审核错误！');
        }

        Db::startTrans();
        try {

            $row->review_id = $userId;
            $row->review_image = $review_image;
            $row->review_remark = $review_remark;
            $row->status = $status;
            $ret = $row->save();

            $rowInfo = \app\admin\model\product\Lists::where('id', $row->product_id)
                ->find();

            if ($rowInfo->stock != $row->after) {
                $before = $rowInfo->stock;
                $rowInfo->stock = $row->after;
                $rowInfo->save();

                $number = abs($before - $row->after);
                $orderNum = create_order($id);
                $change_type = 5;
                $plInfo = \app\admin\model\product\Lists::where('id', $row->product_id)->find();
                $department_id = Category::where('id', $plInfo->category_id)->value('department_id');
                $mdata = [
                    'order_num' => $orderNum,
                    'record_type' => 0,
                    'product_id' => $row->product_id,
                    'change_type' => $change_type,
                    'moneytype' => $plInfo->type,
                    'number' => $number,
                    'hasnumber' => $plInfo->stock,
//                'remark' => $tag,
                    'user_id' => 0,
                    'relation_id' => $id,
                    'targetuserid' => $department_id
                ];

                Change::create($mdata);
            }

            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        if (!$ret) {
            $this->error('审核失败');
        }

        $this->success('审核成功！');
    }
}
